﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoyoEden.Web.Controls;
using CoyoEden;
using CoyoEden.Models;
using System.IO;
using System.Web.Security;

public partial class Views_AlbumList : ViewBase
{
	public string CurrentUser { get; set; }
	protected void Page_Load(object sender, EventArgs e) {
		
		if (!Page.IsPostBack&&!Page.IsCallback) {
			CurrentUser = Page.User.Identity.Name;
			BindAlbums();
			
		}
	}

	private void BindAlbums() {
		string query = Request.QueryString["theme"];
		string theme = !string.IsNullOrEmpty(query) ? query : PortalSettings.Instance.Theme;
		string path = Utils.RelativeWebRoot + "themes/" + theme + "/AlbumView.ascx";

		if (!File.Exists(Request.MapPath(path)))
		{
			path = Utils.RelativeWebRoot + "Views/AlbumView.ascx";//default ascx
		}

		var showAdminLink = false;

		List<Album> albums = new List<Album>();
		if (string.IsNullOrEmpty(CurrentUser)) {
			albums = Album.AlbumsPublic;
		} else if(Page.User.IsInRole("administrators")){
			albums = Album.Albums;
			phToolbox.Visible = true;
			showAdminLink = true;
		}else{
			albums = Album.GetAlbums(CurrentUser);
			phToolbox.Visible = true;
		}

		albums.ForEach(album => {
			AlbumViewBase albumView = (AlbumViewBase)LoadControl(path);
			albumView.TheAlbum = album;
			albumView.ShowAdminLinks = showAdminLink;
			albumsBox.Controls.Add(albumView);
		});
	}
}
